
import { TOOL_CATEGORY_CLASS_IDENTIFIER, TOOL_CLASS_IDENTIFIER, TOOL_PROVIDER_CLASS_IDENTIFIER } from '../../constants';
import { dsFind, dsFindOne, dsInsert, dsUpdateById, dsUpdateByKeys, ComparsionOperator } from '..';

export enum ToolType {
    All = 'all',
    /// buildin
    BuildIn = "buildin",
    /// api
    Api = "api",
    /// wasm
    Wasm = "wasm",
    /// workflow
    Workflow = "workflow",
}

export interface ToolCategory {

}



export interface Tool {
    id: number;

    provider_identifier: string;

    identifier: string;

    name: string;

    title: string;

    icon?: string;

    is_team_authorization: boolean;

    author: string;

    tool_type: ToolType;

    privacy_policy: string;

    custom_disclaimer: string;

    description: string;
}

export const toolGetTools = async (): Promise<{ data: Array<Tool>, total: number }> => {
    try {

        let response = await dsFind(0, TOOL_CLASS_IDENTIFIER,
            {},
            {
                class_selector: {
                    "labels": [
                        TOOL_CATEGORY_CLASS_IDENTIFIER,
                        {

                        },
                        {
                            class_selector: {}
                        }
                    ]
                },
            }
        );

        console.log(response);

        const { data, total } = response;

        return {
            total,
            data: data.map((item, index) => {
                return {
                    id: item.id,
                    provider_identifier: item.provider_identifier,
                    identifier: item.identifier,
                    name: item.name,
                    title: item.title,
                    icon: item.icon,
                    is_team_authorization: item.is_team_authorization,
                    author: item.author,
                    tool_type: item.tool_type,
                    privacy_policy: item.privacy_policy,
                    custom_disclaimer: item.custom_disclaimer,
                    description: item.description,
                };
            })
        };
    } catch (err) {
        console.error(err);
    }
    return {
        total: 0,
        data: []
    };
}

export const toolGetToolCategories = async (): Promise<Array<ToolCategory>> => {
    try {

        let response = await dsFind(0, TOOL_CATEGORY_CLASS_IDENTIFIER,
            {},
            {
                class_selector: {}
            },
        );
        console.log(response);
        const { data, total } = response;

        //   name: string
        //   icon: string
        //   label: TypeWithI18N
        return data.map((item, index) => {

            return {
                id: item.id,
                identifier: item.identifier,
                name: item.name,
                code: item.code,
                label: {
                    'zh_Hans': item.name
                },
                icon: item.icon,
                description: item.description,
            };
        });
    } catch (err) {
        console.error(err);
    }
    return [];
}


export const toolFetchToolsByType = async (toolType: ToolType): Promise<Array<Tool>> => {
    try {

        let response = await dsFind(0, TOOL_CLASS_IDENTIFIER,
            {
                attributes: [
                    {
                        name: "tool_type",
                        operator: ComparsionOperator.Equal,
                        value: toolType
                    }
                ]
            },
            {
                class_selector: {}
            },
        );
        console.log(response);
        const { data, total } = response;
        return data.map((item, index) => {

            return {
                id: item.id,
                provider_identifier: item.provider_identifier,
                identifier: item.identifier,
                name: item.name,
                title: item.title,
                icon: item.icon,
                is_team_authorization: item.is_team_authorization,
                author: item.author,
                tool_type: item.tool_type,
                privacy_policy: item.privacy_policy,
                custom_disclaimer: item.custom_disclaimer,
                description: item.description,
            };
        });
    } catch (err) {
        console.error(err);
    }
    //
    return []
}
